# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Contributor: Xin Sun <sun.simpson@gmail.com>

_realname=hdf5
pkgbase="mingw-w64-${_realname}"
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
_ver=1.14.6
__patch=
pkgver=${_ver}${_patch//-/.}
pkgrel=3
pkgdesc="General purpose library and file format for storing scientific data (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://www.hdfgroup.org/HDF5/"
msys2_repository_url="https://github.com/HDFGroup/hdf5"
msys2_references=(
  "cpe: cpe:/a:hdfgroup:hdf5"
)
license=('spdx:BSD-3-Clause')
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-libaec"
         "${MINGW_PACKAGE_PREFIX}-zlib"
         "${MINGW_PACKAGE_PREFIX}-openssl"
         "${MINGW_PACKAGE_PREFIX}-curl"
         $([[ ${MINGW_PACKAGE_PREFIX} == *-clang-* ]] || echo "${MINGW_PACKAGE_PREFIX}-gcc-libgfortran")
         )
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-fc"
             )
_filever=${_ver//./_}
source=("https://support.hdfgroup.org/releases/hdf5/v${_filever%_*}/v${_filever}/downloads/hdf5-${_ver}${__patch}.tar.gz"
        "hdf5-proper-library-names-mingw.patch"
        "hdf5-fix-static-lib.patch"
        "hdf5-fix-fortran-module-directory-leak.patch"
        "0001-hdf5-pkgconfig-use-requires-private.patch"
        "0002-dynamic-allocation-functions.patch"
        "0003-define-__STDC_WANT_IEC_60559_TYPES_EXT__-early.patch"
        "0005-fix-extracted-library-names.patch"
        "0006-link-static-libs.patch")
sha256sums=('e4defbac30f50d64e1556374aa49e574417c9e72c6b1de7a4ff88c4b1bea6e9b'
            'e236f5805152b25065c206922d7ec1bab05b233adac51bbd0fb1e546326162f6'
            '9d172a7f6b8f54fdbf840e032708acade4ab88c81262b45bfa85d203810962a9'
            '02b5d7335a9db628ed03b29c62d8f0e5f5618fa39de8e453aa02b530d28317a8'
            '101b6b41bd2e3fad9be2b3a72d5a9235fa90d4a900879b687e26521ee1a2fd8c'
            '4119aebc5d8afab3594c58821a104f252fb8c1bcc5bc12292deb53ac48778577'
            '6f9305be4aa849e90e66471100b0480855dc5ac91c4468100ede60127206ad60'
            'c64e938d99ec88b4bb4ec1637e78dcf8aa3b8df592e390d1e85e0b778c3057ea'
            '45b8411d6efe1a7b72ea3767ff9020abeded2d4f7536940795b9d82cd0aeba58')

# Helper macros to help make tasks easier #
apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying ${_patch}"
    patch -Nbp1 -i "${srcdir}/${_patch}"
  done
}

prepare() {
  cd "${srcdir}/${_realname}-${_ver}${__patch}"

  # 0001: curl and openssl in Requires.private, zlib, libaec in Libs.private
  apply_patch_with_msg \
    hdf5-proper-library-names-mingw.patch \
    hdf5-fix-static-lib.patch \
    hdf5-fix-fortran-module-directory-leak.patch \
    0001-hdf5-pkgconfig-use-requires-private.patch \
    0002-dynamic-allocation-functions.patch \
    0003-define-__STDC_WANT_IEC_60559_TYPES_EXT__-early.patch \
    0005-fix-extracted-library-names.patch \
    0006-link-static-libs.patch
}

build() {
  mkdir -p "${srcdir}"/build-${MSYSTEM} && cd "${srcdir}"/build-${MSYSTEM}

  declare -a _extra_config
  if check_option "debug" "n"; then
    _extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    _extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  if [[ ${MINGW_PACKAGE_PREFIX} == *-clang-* ]]; then
    _extra_config+=("-DHDF5_BUILD_FORTRAN=OFF")
  else
    _extra_config+=("-DHDF5_BUILD_FORTRAN=ON")
  fi

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    ${MINGW_PREFIX}/bin/cmake \
      -Wno-dev \
      -GNinja \
      -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
      -DCMAKE_DLL_NAME_WITH_SOVERSION=ON \
      -DCMAKE_C_FLAGS="-Wno-implicit-function-declaration -Wno-error=incompatible-pointer-types" \
      -DBUILD_TESTING=OFF \
      "${_extra_config[@]}" \
      -DCMAKE_SKIP_RPATH=ON \
      -DHDF5_BUILD_CPP_LIB=ON \
      -DHDF5_BUILD_HL_LIB=ON \
      -DHDF5_BUILD_TOOLS=ON \
      -DHDF5_ENABLE_DEPRECATED_SYMBOLS=ON \
      -DHDF5_ENABLE_SZIP_SUPPORT=ON \
      -DHDF5_ENABLE_Z_LIB_SUPPORT=ON \
      -DZLIB_ROOT=${MINGW_PREFIX} \
      -DHDF5_ENABLE_ROS3_VFD=ON \
      -DHDF5_INSTALL_CMAKE_DIR="lib/cmake" \
      -DHDF5_INSTALL_DATA_DIR="share/doc/hdf5" \
      -DHDF5_INSTALL_MODULE_DIR="include" \
      -DHDF5_MSVC_NAMING_CONVENTION=OFF \
      ../${_realname}-${_ver}${__patch}

  ${MINGW_PREFIX}/bin/cmake --build .
}

package() {
  cd "${srcdir}/build-${MSYSTEM}"

  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake --install .

  local PREFIX_WIN=$(cygpath -wm ${MINGW_PREFIX})
  for _f in "${pkgdir}${MINGW_PREFIX}"/lib/cmake/*.cmake; do
    sed -e "s|${PREFIX_WIN}|\$\{_IMPORT_PREFIX\}|g" -i ${_f}
  done

  for pcfile in "${pkgdir}${MINGW_PREFIX}"/lib/pkgconfig/*.pc; do
    sed -s "s|${PREFIX_WIN}|${MINGW_PREFIX}|g" -i "${pcfile}"
  done
  sed -s "s|${PREFIX_WIN}|${MINGW_PREFIX}|g" -i "${pkgdir}${MINGW_PREFIX}"/lib/libhdf5.settings

  install -d "${pkgdir}"${MINGW_PREFIX}/share/licenses/${_realname}
  install -Dm644 "${srcdir}"/${_realname}-${_ver}${__patch}/COPYING* \
    -t "${pkgdir}"${MINGW_PREFIX}/share/licenses/${_realname}
}
